<?php
/**
 * Created by PhpStorm.
 * User: 86182
 * Date: 2020/6/22
 * Time: 23:37
 */

namespace App\Utils;

class Category
{


    public static function getTree($data){
        //第一步 构造数据
        $items = array();
        foreach($data as $value){
            $items[$value['id']] = $value;
        }
        //第二部 遍历数据 生成树状结构
        $tree = array();
        foreach($items as $key => $item){
            if(isset($items[$item['pid']])){
                $items[$item['pid']]['children'][] = &$items[$key];
            }else{
                $tree[] = &$items[$key];
            }
        }
        return $tree;
    }

    function changeKeys($array, $keyEnArray, $keyZhCnArray)
    {
        if(!is_array($array)) return $array;
        $tempArray = array();
        foreach ($array as $key => $value){
            // 处理数组的键，翻译成中文
            $key = array_search($key, $keyEnArray, true) === false ? $key : $keyZhCnArray[array_search($key, $keyEnArray)];
            if(is_array($value)){
                $value = changeKeys($value, $keyEnArray, $keyZhCnArray);
            }
            $tempArray[$key] = $value;
        }
        return $tempArray;
    }


    //无限级分类树状显示使用
    public function unlimitedForLevel($cate_list,$html='--',$pid=0,$level=0){
        $arr=array();
        foreach($cate_list as $v){
            if($v['parentid']==$pid){
                $v['level']=$level+1;
                $v['html']='<e class="catelist_bg"></e>'.str_repeat($html,$level);
                $arr[]=$v;
                $arr=array_merge($arr,self::unlimitedForLevel($cate_list,$html,$v['id'],$level+1));
            }
        }
        return $arr;
    }

    //无限级分类树状显示，适合select使用
    public static function unlimitedForLevel2($cate_list,$html='--',$pid=0,$level=0){
        $arr=array();
        foreach($cate_list as $v){
            if($v['pid']==$pid){
                $v['level']=$level+1;
                $v['html']='|'.str_repeat($html,$level);
                $arr[]=$v;
                $arr=array_merge($arr,self::unlimitedForLevel2($cate_list,$html,$v['id'],$level+1));
            }
        }
        return $arr;
    }


    /*
    $request_id,当前修改的分类的主键id,即url请求的参数id
    如果当前遍历的数据的id与request_id相等,则跳过该条数据继续遍历
     */
    public function unlimitedForLevelForEdit($cate_list,$html='--',$pid=0,$level=0,$request_id){
        $arr=array();
        foreach($cate_list as $v){
            if($v['id']  == $request_id){
                continue;
            }
            if($v['parentid']==$pid){
                $v['level']=$level+1;
                $v['html']='|—'.str_repeat($html,$level);
                $arr[]=$v;
                $arr=array_merge($arr,self::unlimitedForLevelForEdit($cate_list,$html,$v['id'],$level+1,$request_id));
            }
        }
        //exit;
        return $arr;
    }

    //传递一个子分类的id获取所有的父级分类
    public function getParentCatelist($cate_list,$id=0){
        $arr=array();
        foreach($cate_list as $v){
            if($v['id']==$id){
                $arr[]=$v;
                $arr=array_merge(self::getParentCatelist($cate_list,$v['parentid']),$arr);
            }
        }
        return $arr;
    }
    //传递一个父级分类的id获取所有的子级分类id，查看某一分类及其子分类下的所有文章时很有用,where (cate_id=1 or cate_id in (2,4,5))
    public function getChildCatelist($cate_list,$pid=0){
        $arr=array();
        foreach($cate_list as $v){
            if($v['parentid']==$pid){
                $arr[]=$v['id'];
                $arr=array_merge($arr,self::getChildCatelist($cate_list,$v['id']));
            }
        }
        return $arr;
    }

    //传递一个父级分类的id获取所有的子级分类id，查看某一分类及其子分类下的所有文章时很有用,where (cate_id=1 or cate_id in (2,4,5))
    public function getChildCatelist1($cate_list,$pid=0){
        $arr=array();
        foreach($cate_list as $v){
            if($v['parentid']==$pid){
                $arr[]=$v['id'];
                $arr=array_merge($arr,self::getChildCatelist($cate_list,$v['id']));
            }
        }
        return array_merge([$pid],$arr);
    }

    public function findChild(&$arr,$id){
        $childs=array();
        foreach ($arr as $k => $v){
            if($v['parentid']== $id){
                $childs[]=$v;
            }
        }

        return $childs;
    }

    public function getJsonTree($rows,$root_id){
        $childs=$this->findChild($rows,$root_id);
        if(empty($childs)){
            return null;
        }else{
            foreach ($childs as $k => $v){
                $rescurTree=$this->getJsonTree($rows,$v['id']);
                if( null != $rescurTree){
                    $childs[$k]['children']=$rescurTree;
                }
            }
        }
        return $childs;
    }

    public function cutField($arr=[]){
        foreach ($arr as $k=>$v){
            unset($arr[$k]['parentid']);
            if(!empty($arr[$k['children']])){
                $this->cutField($arr[$k['children']]);
            }
        }
    }


    public function dealMenuTagDelete(&$menuData, $tag_id)   {
        if (!is_array($menuData)) {
            return false;
        }
        foreach ($menuData as $key => $val) {
            if ($key === 'old_tags_id') {
                foreach ($val as $tagKey => $tagVaule) {
                    if ($tagVaule == $tag_id) {
                        unset($menuData['old_tags_id'][$tagKey]);
                        unset($menuData['old_tags'][$tagKey]);
                    }
                }
            }
            if (is_array($val)) {
                self::dealMenuTagDelete($menuData[$key], $tag_id);
            }
        }
        return $menuData;
    }



}